// RedTube.js for PSPTube
// Made By JK108
// GetURL Function By Freeplay

function RedTube_CheckURL( url, option )
{
	if (url.match("http://www.redtube.com") == "http://www.redtube.com"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a RedTube URL
*/
function RedTube_GetURL( url, option )
{
	// Grab video ID:
	// "movie=" video ID
	var video_id = url.match( /^http:\/\/www\.redtube\.com\/([0-9]*)/ );
	if(video_id == null) { return null; /* ID not valid */ }
	video_id = RegExp.$1;

	var contents = GetContents( "http://www.redtube.com/" + video_id );
	if(contents == null) { return null; /* error getting page */ }

	var s = "" + video_id;
	if (s == "")
	{
		s = "1";
	} // end if
	var pathnr = "" + Math.floor(parseFloat(s) / 1000);
	var l = s.length;
	var i = 1;
	while (i <= 7 - l)
	{
		s = "0" + s;
		++i;
	} // end while
	l = pathnr.length;
	for (i = 1; i <= 7 - l; i++)
	{
		pathnr = "0" + pathnr;
	} // end of for
	var xc = new Array("R", "1", "5", "3", "4", "2", "O", "7", "K", "9", "H", "B", "C", "D", "X", "F", "G", "A", "I", "J", "8", "L", "M", "Z", "6", "P", "Q", "0", "S", "T", "U", "V", "W", "E", "Y", "N");
	var code = "";
	var qsum = 0;
	for (i = 0; i <= 6; i++)
	{
		qsum = qsum + parseInt(s.charAt(i)) * (i + 1);
	} // end of for
	var s1 = "" + qsum;
	qsum = 0;
	for (i = 0; i < s1.length; i++)
	{
		qsum = qsum + parseInt(s1.charAt(i));
	} // end of for
	var qstr;
	if (qsum >= 10)
	{
		qstr = "" + qsum;
	}
	else
	{
		qstr = "0" + qsum;
	} // end else if
	code = code + xc[s.charCodeAt(3) - 48 + qsum + 3];
	code = code + qstr.charAt(1);
	code = code + xc[s.charCodeAt(0) - 48 + qsum + 2];
	code = code + xc[s.charCodeAt(2) - 48 + qsum + 1];
	code = code + xc[s.charCodeAt(5) - 48 + qsum + 6];
	code = code + xc[s.charCodeAt(1) - 48 + qsum + 5];
	code = code + qstr.charAt(0);
	code = code + xc[s.charCodeAt(4) - 48 + qsum + 7];
	code = code + xc[s.charCodeAt(6) - 48 + qsum + 4];
	content_video = pathnr + "/" + code + ".flv";
	return "http://dl.redtube.com/_videos_t4vn23s9jc5498tgj49icfj4678/"+content_video;
}

function RedTube_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;	// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;
	var perpage = 20;


	result.start = (((result.start - 1) / 10) * perpage) + 1;


	if(result.start < perpage){
	var contents = GetContents( "http://www.redtube.com/?search=" + keyword.replace(/ /g, "+") );
	} else{
	var num = 1 + ((result.start - 1) / perpage);
	var contents = GetContents( "http://www.redtube.com/?search=" + keyword.replace(/ /g, "+") + "&page=" + num );
	}
	if(contents == null) { return null; /* failed to get page */ }


	if(contents.match("font-family:Arial;'>No Videos found.</span><br>") == "font-family:Arial;'>No Videos found.</span><br>"){ result.total = 0; return result; }
	var total = contents.match(/font-family\:Verdana,Tahoma,Arial;\'\>(.*?)\<span style\=/);
	var total = total[1].match(/ of (.*?) videos/);
	var total = total[1].replace(/,/g, "");
	result.total = total;

	var origcontents = contents;


	while(1){
	var pos = contents.indexOf("<a class=s target=");

	if(pos != -1){
		contents = contents.substring(pos + 18, contents.length);
		//Get URL
		var url = contents.match(/href\=\'(.*?)\'/);
		var url = url[1];
		//Title
		var title = contents.match(/\'\>(.*?)\</);
		var title = ImproveText(title[1]);
		//Description
		var description = contents.match(/(.*?)/);
		var description = splitTo2Lines(title, 60);

		info = new Object();
		info.URL = url;
		info.Title = title;
		info.Description = description;
		info.SaveFilename = ToFilename(title);
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.VideoInfo.length == perpage){break;}
		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	contents = origcontents;
	var counter = 0;
	while(1){
	var pos = contents.indexOf("<a target=_blank href='");
	if(pos != -1){
		contents = contents.substring(pos + 23, contents.length);
		//Get Thumbnail
		var thumb_url = contents.match(/ src\=\'(.*?)\'/);
		var thumb_url = thumb_url[1];
		result.VideoInfo[counter].ThumbnailURL = thumb_url;
	} else{
		break;
	}
	counter += 1;
	}


	contents = origcontents;
	counter = 0;
	while(1){
	var pos = contents.indexOf("<table border=0 cellspacing=0 cellpadding=0 width=100%><tr><td><span ");
	if(pos != -1){
		contents = contents.substring(pos + 69, contents.length);
		//Length
		var length = contents.match(/class\=d\>(.*?)\<\/span\>/);
		var length = TimestringToSeconds(length[1]);
		//Rating
		var rating = contents.match(/\<\/span\>\<\/td\>\<td align\=right\>(.*?)\<\/td\>/);
		var rating = parseFloat(rating[1]);
		result.VideoInfo[counter].LengthSeconds = length;
		result.VideoInfo[counter].RatingAvg = rating;
	} else{
		break;
	}
	counter += 1;
	}


	contents = origcontents;
	counter = 0;
	while(1){
	var pos = contents.indexOf("<table border=0 cellspacing=0 cellpadding=0 width=100%><tr><td valign=mid");
	if(pos != -1){
		contents = contents.substring(pos + 73, contents.length);
		//Views
		var views = contents.match(/\>(.*?) views/);
		var views = parseInt(views[1].replace(/,/g, ""));
		result.VideoInfo[counter].ViewCount = views;
	} else{
		break;
	}
	counter += 1;
	}


	if(result.VideoInfo.length) {
		result.end = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != perpage){result.total = result.end;}

	return result;
}

// Service metadata
var RedTube = new Object();
RedTube.Name          = "RedTube";		/* Service name */
RedTube.Description   = "RedTube";		/* Service description */
RedTube.SearchDesc    = "RedTube";		/* Service search screen description */
RedTube.SearchOSKMode = 1;			/* OSK mode 1: English only  2: Japanese */
RedTube.CheckURL      = RedTube_CheckURL;	/* CheckURL function */
RedTube.GetURL        = RedTube_GetURL;		/* GetURL function */
RedTube.Search        = RedTube_Search;		/* Search function */

// add RedTube to site list
SiteList.push( RedTube );
